#! /usr/bin/env bash

# -----BEGIN COMMON.SH-----
# -----END COMMON.SH-----

export DEBIAN_FRONTEND=noninteractive

set -euo pipefail

apt-get update --yes
apt-get --yes install git cmake ninja-build autotools-dev \
	    build-essential g++ clang python-dev \
	    libicu-dev libbz2-dev \
	    locales wget curl apt-utils \
	    lsb-release
apt-get --yes install xorg xvfb xauth xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic
apt-get --yes install libqt5gui5 libqt5core5a libqt5dbus5 qttools5-dev qttools5-dev-tools libprotobuf-dev protobuf-compiler
apt remove --yes libboost-all-dev
apt autoremove --yes

# Ensure we have a new enough Boost
if ! have boost; then
	bootstrap_boost -m -k
fi

if ! have boost; then
	echo "Unable to install boost" >&2

	exit 1
fi

if ! version_min 'boost --version' 1.66.999; then
	echo "boost version too low (1.67.0+ required)" >&2
	exit 1
fi
boost_dir="$(boost --install-prefix)"

# Ensure we have a new enough Qt5
if ! have qtpaths; then
	echo "Unable to install qt5" >&2

	exit 1
fi

if ! version_min 'qtpaths --qt-version 2>/dev/null' 5.2.999 5.8.999; then
	if ! version_min 'qtpaths -- --qt-version' 5.2.999 5.8.999; then
		echo "qt5 version not in range (version range: [5.3+, 5.9-))" >&2

		exit 1
	fi
fi
qt5_dir="$(qtpaths --install-prefix)/lib/cmake/Qt5"

if [ ! -d "${qt5_dir}" ]; then
	qt5_dir=''
fi

# Determine how to call cmake
cmake_add=''
if [ -n "${qt5_dir}" ]; then
	cmake_add="${cmake_add} -DQt5_DIR=${qt5_dir}"
fi

echo "All verified."
echo ""
echo "Next steps:"
echo "    cmake -DBOOST_ROOT=${boost_dir} -DNANO_GUI=ON ${cmake_add} <dir>"
echo "    cpack -G \"TBZ2\""

exit 0
